package shoppingmall.manage.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shoppingmall.manage.pojo.CommodityCategory;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.dto.Result;
import shoppingmall.manage.service.CommodityCategoryService;

import java.util.List;

/**
 * 商品分类管理相关 controller层
 */
@Slf4j
@RestController
public class CommodityCategoryController {

    @Autowired
    private CommodityCategoryService commodityCategoryService;

    /**
     * 分页查询商品分类条数
     */
    @GetMapping("/manage/category")
    public Result getCommodityCategoryList (@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        log.info("分页查询商品分类:{},{}",page,pageSize);
        //调用service分页查询商品分类
        PageBean commodityCategoryList = commodityCategoryService.getCommodityCategoryList(page, pageSize);
        if(commodityCategoryList.getTotal() == 0) {
            return Result.error("未查询到商品分类数据!");
        }
        return Result.success(commodityCategoryList);
    }

    /**
     * ***查询所有商品分类
     */
    @GetMapping("/manage/category/all")
    public Result getCommodityCategoryList () {
        log.info("查询所有的商品分类");
        //调用service分页查询商品分类
        PageBean commodityCategoryList = commodityCategoryService.getAllCommodityCategoryList();
        if(commodityCategoryList.getTotal() == 0) {
            return Result.error("未查询到商品分类数据!");
        }
        return Result.success(commodityCategoryList);
    }

    /**
     * 添加一条商品分类
     */
    @PostMapping("/manage/category")
    public Result saveCommodityCategory (@RequestBody CommodityCategory commodityCategory) {
        log.info("添加一条商品分类:{}",commodityCategory.getCategoryName());
        boolean isSave = commodityCategoryService.saveCommodityCategory(commodityCategory.getCategoryName());
        if(!isSave){
            return Result.error("商品分类添加失败!");
        }
        return Result.success("商品分类添加成功!");
    }

    /**
     * 修改一条商品分类
     */
    @PutMapping("/manage/category")
    public Result updateCommodityCategory (@RequestBody CommodityCategory commodityCategory) {
        log.info("修改一条商品分类:{}",commodityCategory.getCategoryName());
        boolean idUpdate = commodityCategoryService.updateCommodityCategory(commodityCategory);
        if(!idUpdate){
            return Result.error("商品分类更新失败!");
        }
        return Result.success("商品分类修改成功!");
    }

    /**
     * 删除一条商品分类
     */
    @DeleteMapping("/manage/category")
    public Result deleteCommodityCategory (@RequestParam int categoryId) {
        log.info("删除一条商品分类:{}",categoryId);
        boolean isDel = commodityCategoryService.deleteCommodityCategory(categoryId);
        if(!isDel){
            return Result.error("商品分类删除失败!");
        }
        return Result.success("商品分类删除成功!");
    }

    /**
     * 批量删除商品分类
     */
    @DeleteMapping("/manage/category/batch")
    public Result deleteCommodityCategoryBatch (@RequestBody List<Integer> categoryIds) {
        log.info("批量删除商品分类，ids = {}",categoryIds.toString());
        //调用服务层方法
        boolean isDel = commodityCategoryService.deleteCommodityCategoryBatch(categoryIds);
        if(!isDel) {
            return Result.error("批量删除商品分类失败!");
        }
        return Result.success("批量删除商品分类成功!");
    }
}
